package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.GrouponRules;
import com.cskaoyan.service.GrouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("admin/groupon")
public class GrouponController {

    @Autowired
    GrouponService grouponService;

    @GetMapping("list")
    public BaseRespVo listGroupon(Integer page,Integer limit,
                                  Integer goodsId,String sort,String order){
        Map<String,Object> map = grouponService.listGroupon(page,limit,goodsId,sort,order);
        return BaseRespVo.ok(map);
    }
    @PostMapping("create")
    public BaseRespVo publishGroupon(@RequestBody GrouponRules grouponRules){
        grouponRules = grouponService.publishGroupon(grouponRules);
        return BaseRespVo.ok(grouponRules);
    }
    @PostMapping("update")
    public BaseRespVo editGroupon(@RequestBody GrouponRules grouponRules){
        int update = grouponService.editGroupon(grouponRules);
        if (update != 1){
            return BaseRespVo.fail("系统繁忙");
        }
        return BaseRespVo.ok();
    }
    @PostMapping("delete")
    public BaseRespVo deleteGroupon(@RequestBody GrouponRules grouponRules){
        int delete = grouponService.deleteGroupon(grouponRules);
        if (delete != 1){
            return BaseRespVo.fail("系统繁忙");
        }
        return BaseRespVo.ok();
    }
}
